<template>
  <el-dialog :model-value="props.dialogTableVisible" title="userId? '修改':'新建'">
    <el-form class="register-form" :model="user" ref="userRef" label-width="120px" :rules="registerRules" label-position="top">
      <el-form-item label="账号" prop="userName">
        <el-input min="10" max="16" v-model="user.userName" name="userName" />
      </el-form-item>
      <el-form-item label="姓名" prop="userInfo.name">
        <el-input min="10" max="16" v-model="user.userInfo.name" name="name" />
      </el-form-item>
      <el-form-item label="昵称" prop="nikeName">
        <el-input min="10" max="16" v-model="user.userInfo.nikeName" name="nikeName" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input max="30" v-model="user.email" name="email"/>
      </el-form-item>
      <el-form-item label="联系方式" prop="tel">
        <el-input type="number"  min="11" max="11" v-model="user.tel" name="tel"/>
      </el-form-item>
      <el-form-item label="性别" prop="userInfo.sex">
        <el-select v-model.number="user.userInfo.sex" placeholder="" name="sex">
          <el-option label="男" value="0" />
          <el-option label="女" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="证件类型" prop="userInfo.cardType">
        <el-select v-model="user.userInfo.cardType" placeholder="" name="cardType">
          <el-option label="身份证" value="0" />
          <el-option label="护照" value="1" />
          <el-option label="户口簿" value="2" />
          <el-option label="港澳居住证" value="3" />
          <el-option label="台湾临时居住证" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="出生日期" required>
        <el-col :span="12">
          <el-form-item prop="userInfo.birthDate">
            <el-date-picker
              v-model="user.userInfo.birthDate"
              type="date"
              placeholder=""
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="证件号码" prop="userInfo.cardCode">
        <el-input min="10" max="16" v-model="user.userInfo.cardCode" name="cardCode" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup>
import { defineProps, ref, defineEmits, onMounted } from 'vue'
import { getById } from '@/api/common/common'
import { useI18n } from 'vue-i18n'
import { useStore } from 'vuex'

const i18n = useI18n()
const props = defineProps({
  userId: {
    type: Number,
    require: false
  },
  dialogTableVisible: {
    type: Boolean,
    require: true
  }
})

const emits = defineEmits(['update:dialogTableVisible'])

const user = ref({
  userName: '',
  email: '',
  tel: '',
  userInfo: {
    name: '',
    nikeName: '',
    age: 0,
    sex: '',
    birthDate: new Date(),
    cardType: '',
    cardCode: ''
  }
})

onMounted(() => {
  if (props.userId) {
    getById(props.userId, 'user').then(res => {
      console.log(res)
      return res.data
    })
  }
  return {
    userName: '',
    email: '',
    tel: '',
    userInfo: {
      name: '',
      nikeName: '',
      age: 0,
      sex: '',
      birthDate: new Date(),
      cardType: '',
      cardCode: ''
    }
  }
})

console.log(user.value)
const userRef = ref(user.value)

const registerRules = ref({
  userName: [
    {
      required: true,
      trigger: 'blur',
      message: '用户名不能为空'
    }
  ],
  email: [
    {
      required: true,
      trigger: 'blur',
      message: '邮箱不能为空'
    },
    {
      type: 'email',
      trigger: 'blur',
      message: '格式不对'
    }
  ],
  tel: [
    {
      required: true,
      trigger: 'blur',
      message: '联系方式不对'
    }
  ],
  'userInfo.birthDate': [
    {
      type: 'date',
      required: true,
      trigger: 'change',
      message: '出生日期不能为空'
    }
  ],
  'userInfo.name': [
    {
      required: true,
      trigger: 'blur',
      message: '姓名不能为空'
    }
  ],
  'userInfo.sex': [
    {
      required: true,
      trigger: 'change',
      message: '性别不能为空'
    }
  ],
  'userInfo.cardCode': [
    {
      required: true,
      trigger: 'blur',
      message: '证件号不能为空'
    }
  ],
  'userInfo.cardType': [
    {
      required: true,
      trigger: 'change',
      message: '证件类型不能为空'
    }
  ]
})

const onSubmit = () => {
  console.log('onsubmit')
}

const onCancel = () => {
  emits('update:dialogTableVisible', false)
}
</script>

<style lang="scss" scoped>
.register-form {
  position: relative;
  width: 90%;
  max-width: 100%;
  top: 50px;
  left: 40px;
}
</style>
